package com.gome.han.lucene.analyzer;

import java.io.IOException;
import java.io.StringReader;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.Version;


/**
* @author hanpeng
* @version 创建时间：2021年2月1日 下午5:16:34
* @ClassName 类名称
* @Description 类描述
*/
public class FieldSectionBoundaries {

	public static void main(String[] args) throws IOException, ParseException {
		StandardAnalyzer analyzer = new StandardAnalyzer();
		//analyzer.getPositionIncrementGap(fieldName);
		Path rootDirectory = FileSystems.getDefault().getPath("D:/es/lucene/practice/tempIndex");
		Path indexPath = Files.createTempDirectory(rootDirectory, "");
		Directory directory = FSDirectory.open(indexPath);
		IndexWriterConfig config = new IndexWriterConfig(analyzer);
		IndexWriter iwriter = new IndexWriter(directory, config);
		Document doc = new Document();
		//比如商品id=5的标题是”男士美白牙膏“，  搜”牙膏"能返回id=5，但是不能返回”男士美白牙膏" TYPE_NOT_STORED 不能做为返回字段，  只能用于检索
		doc.add(new Field("f","first ends",TextField.TYPE_STORED));//0 5 和6 10
		doc.add(new Field("f","starts two",TextField.TYPE_STORED));//starts  11 17  
		iwriter.addDocument(doc);
		iwriter.close();

		// Now search the index:
		DirectoryReader ireader = DirectoryReader.open(directory);
		IndexSearcher isearcher = new IndexSearcher(ireader);
		// Parse a simple query that searches for "text":
		QueryParser parser = new QueryParser("fieldname", analyzer);
		Query query = parser.parse("text");
		ScoreDoc[] hits = isearcher.search(query, 10).scoreDocs;
		// assertEquals(1, hits.length);
		// Iterate through the results:
		for (int i = 0; i < hits.length; i++) {
			Document hitDoc = isearcher.doc(hits[i].doc);
			//assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
		}
		ireader.close();
		directory.close();
		IOUtils.rm(indexPath);
		/* Version matchVersion = Version.LUCENE_XY; // Substitute desired Lucene version for XY
		   Analyzer myAnalyzer = new StandardAnalyzer(matchVersion) {
		     public int getPositionIncrementGap(String fieldName) {
		       return 10;  //默认为 0
		     }
		   };*/

	}

}
